package com.hqyj.controller;

import com.hqyj.pojo.Payment;
import com.hqyj.serviceInf.PaymentServiceInf;
import com.hqyj.serviceInf.UserServiceInf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/payment")
public class PaymentController {
    @Autowired
    private PaymentServiceInf paymentServiceinf;
    private UserServiceInf userServiceInf;
    //全查
  @RequestMapping("/paymentList")
  @ResponseBody
  public Map<String,Object> getAllPayment(@RequestParam(defaultValue = "1", required = false)int pageNum,
                                        @RequestParam(defaultValue = "3", required = false)int pageSize ,
                                        @RequestParam(defaultValue = "")String username){
      return paymentServiceinf.selectAll(pageNum, pageSize,username);
  }
    //添加
    @RequestMapping("/addpayment")
    public String insertPayment(Payment payment){
        return paymentServiceinf.insertPayment(payment)>0?"redirect:order-list":"order-add";
    }
    //查询一个
    @RequestMapping("/showpayment")
    public String showpayment(int paymentid, Model model){
      Payment payment = paymentServiceinf.selectPaymentByPaymentId(paymentid);
      model.addAttribute("payment",payment);
        return "payment-update";
    }
    //更改
    @RequestMapping(value = "/updatePayment", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updatePayment(@RequestBody Payment payment){
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", -1);
        map.put("message", "修改失败");
        if(paymentServiceinf.updatePayment(payment) > 0){
            map.put("code", 200);
            map.put("message", "修改成功");
            map.put("url", "/payment/paymentList");
        }
        return map;
    }
    //删除
    @RequestMapping("/deletepayment")
//    @ResponseBody
    public String deletepayment(int paymentid,Model model) {
        if (paymentServiceinf.deletePayment(paymentid) > 0) {
            model.addAttribute("message", "删除成功");
        } else {
            model.addAttribute("message", "删除失败");
        }
        return "redirect:paymentList";
    }

    //页面通用跳转方法
    @RequestMapping("{page}")
    public String toPage(@PathVariable()String page){
        return page;
    }

}
